package com.huisang.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huisang.domain.Customer;
import com.huisang.domain.Emp;
import com.huisang.domain.Track;
import com.huisang.domain.TrackVo;
import com.huisang.mapper.CustomerMapper;
import com.huisang.mapper.EmpMapper;
import com.huisang.mapper.TrackMapper;
import com.huisang.service.TrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TrackServiceImpl implements TrackService {
    @Autowired
    private TrackMapper trackMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public int addTrack(Track track) {
        int i = trackMapper.addTrack(track);
        return i;
    }

    @Override
    public int deleteTrack(int tid) {
        int i = trackMapper.deleteTrack(tid);
        return i;
    }

    @Override
    public int updateTrack(Track track) {
        int i = trackMapper.updateTrack(track);
        return i;
    }

    @Override
    public PageInfo<TrackVo> findAllTrack(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Track> allTrack = trackMapper.findAllTrack();
        PageInfo<Track> pageInfo = new PageInfo<Track>(allTrack);

        List<TrackVo> list = new ArrayList<>();
        for (Track track : allTrack) {
            TrackVo trackVo = new TrackVo();
            Customer customer = customerMapper.findOneCustomerById(track.getCid());
            Emp emp = empMapper.finEmpByEid(track.getEid());
            //密码置为空
            emp.setPasswd("");
            trackVo.setTid(track.getTid());
            trackVo.setCustomer(customer);
            trackVo.setEmp(emp);
            trackVo.setRecord(track.getRecord());
            trackVo.setIntention(track.getIntention());
            list.add(trackVo);
        }
        PageInfo<TrackVo> pageInfoVo = new PageInfo<>(list);
        pageInfoVo.setTotal(pageInfo.getTotal());
        pageInfoVo.setPageNum(pageInfo.getPageNum());
        pageInfoVo.setPages(pageInfo.getPages());
        pageInfoVo.setSize(pageInfo.getSize());
        return pageInfoVo;
    }

    @Override
    public Track findOneTrack(int tid) {
        Track track = trackMapper.findOneTrack(tid);
        return track;
    }
}
